package aryn.framework.math;

import java.awt.Point;

public class Parable {
	
	// parable with vertical axis
	// of the type y = ax^2 + bx + c
	
	private double a;
	private double b;
	private double c;
	
	private double delta;
	
	public Parable(Point p1, Point p2, Point p3) {
		double i = p1.y;	double l = p1.x;
		double j = p2.y;	double m = p2.x;
		double k = p3.y;	double n = p3.x;
		
		this.a = ( i*(m-n) + j*(n-l) + k*(l-m) ) / ( (l-m)*(l-n)*(m-n) );
		this.b = ( i*(m+n)*(m-n) + j*(n+l)*(n-l) + k*(l+m)*(l-m) ) / ((l-m)*(l-n)*(n-m));
		this.c = ( i*m*n*(m-n) - l*(j*n*(l-n) + k*m*(m-l))) / ((l-m)*(l-n)*(m-n)) ;
		this.delta = b*b - 4*a*c;
	}
	
	public Parable(double p1x, double p1y, double p2x, double p2y, double p3x, double p3y) {
		double i = p1y;		double l = p1x;
		double j = p2y;		double m = p2x;
		double k = p3y;		double n = p3x;
		
		this.a = ( i*(m-n) + j*(n-l) + k*(l-m) ) / ( (l-m)*(l-n)*(m-n) );
		this.b = ( i*(m+n)*(m-n) + j*(n+l)*(n-l) + k*(l+m)*(l-m) ) / ((l-m)*(l-n)*(n-m));
		this.c = ( i*m*n*(m-n) - l*(j*n*(l-n) + k*m*(m-l))) / ((l-m)*(l-n)*(m-n)) ;
		this.delta = b*b - 4*a*c;
	}
	
	public Parable(double a, double b, double c) {
		this.a = a;
		this.b = b;
		this.c = c;
		this.delta = b*b - 4*a*c;
	}
	
	public double getY(int x) {
		return a*x*x + b*x + c;
	}
	
	public double getXmin(int y) {
		double delta1 = b*b - 4*a*(c - y);
		if (delta1 < 0) throw new ArithmeticException("delta < 0");
		return (-b - Math.sqrt(delta)) / 2*a;
	}
	
	public double getXmax(int y) {
		double delta1 = b*b - 4*a*(c - y);
		if (delta1 < 0) throw new ArithmeticException("delta < 0");
		return (-b + Math.sqrt(delta)) / 2*a;
	}
	
	
}
